package com.yoyoyo666.report.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoyoyo666.report.common.AnnoLogin;
import com.yoyoyo666.report.common.IDUtils;
import com.yoyoyo666.report.mapper.ReportCorrelationUserMapper;
import com.yoyoyo666.report.mapper.ReportUserMapper;
import com.yoyoyo666.report.model.LoginUser;
import com.yoyoyo666.report.model.ReportCorrelationUser;
import com.yoyoyo666.report.model.ReportUser;
import com.yoyoyo666.report.model.common.BaseResult;
import com.yoyoyo666.report.model.common.SingletonResult;
import com.yoyoyo666.report.zento.mapper.BugDao;
import com.yoyoyo666.report.zento.model.BugDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {


    @Autowired
    private ReportUserMapper reportUserMapper;

    @Autowired
    private BugDao bugDao;


    @Autowired
    private ReportCorrelationUserMapper reportCorrelationUserMapper;

    @PostMapping("login")
    public SingletonResult<LoginUser> login(HttpSession session, String username , String password){

        ReportUser login = reportUserMapper.login(username, password);

        if(login==null){
           return  SingletonResult.getSingletonFail("用户名/密码 错误");
        }
        List<ReportCorrelationUser> byUserId = reportCorrelationUserMapper.getByUserId(login.getId());
        List<ReportUser> l = new ArrayList<ReportUser>();
        for (ReportCorrelationUser reportCorrelationUser : byUserId) {
            ReportUser u = reportUserMapper.selectByPrimaryKey(reportCorrelationUser.getCorrelation());
            u.setPassword(null);
            u.setMailPwd(null);
            l.add(u);
        }
        LoginUser user = new LoginUser();
        user.setCurrentUser(login);
        user.setCorrelationUsers(l);
        l.add(login);
        for (ReportUser reportUser : l) {
            reportUser.setMailPwd(null);
            reportUser.setPassword(null);
        }
        session.setAttribute("user",user);
        return  SingletonResult.getSuccess(user);
    }

    @GetMapping("loginOut")
    public BaseResult loginOut(HttpSession session){

        session.removeAttribute("user");

        return BaseResult.getSuccess();
    }

    @PostMapping("changeCurrent")
    public BaseResult changCurrent(HttpSession session, String userId){
        LoginUser user = (LoginUser)session.getAttribute("user");

        for (ReportUser reportUser : user.getCorrelationUsers()) {
            if(reportUser.getId().equals(userId)){
                user.setCurrentUser(reportUser);
                return BaseResult.getSuccess();
            }
        }

        return  BaseResult.getFail();
    }

    @PostMapping("add")
    public BaseResult add(ReportUser user){

        BaseResult baseResult = validationUser(user, null);
        if(!baseResult.isResult()){
            return baseResult;
        }
        user.setId(IDUtils.getId());
        int insert = reportUserMapper.insert(user);

        if(insert>0){
            return BaseResult.getSuccess();
        }
        return  BaseResult.getFail();
    }

    @PostMapping("modify")
    public BaseResult modify(@AnnoLogin LoginUser loginUser,ReportUser user){

        BaseResult baseResult = validationUser(user, loginUser);

        if(!baseResult.isResult()){
            return baseResult;
        }

        user.setId(loginUser.getCurrentUser().getId());

        int insert = reportUserMapper.updateByPrimaryKey(user);

        if(insert>0){
            return BaseResult.getSuccess();
        }
        return  BaseResult.getFail();
    }


    private BaseResult validationUser(ReportUser user,LoginUser loginUser){

        StringBuffer err  = new StringBuffer();

        if(StringUtils.isBlank(user.getName())){
            err.append("名称为空");
        }
        if(StringUtils.isBlank(user.getUsername())){
            err.append("用户名为空");
        }
        if(StringUtils.isBlank(user.getPassword())){
            err.append("密码为空");
        }
        if(StringUtils.isBlank(user.getMailName())){
            err.append("邮箱为空");
        }
        if(StringUtils.isBlank(user.getMailPwd())){
            err.append("邮箱密码为空");
        }

        if(loginUser != null){
           if( !user.getUsername().equals(user.getUsername())){
               err.append("用户名称不可修改");
           }
        }else{
            ReportUser reportUser = reportUserMapper.selectByUserName(user.getUsername());
            if(reportUser != null){
                err.append("用户名称重复");
            }
        }

        if(err.length()>0){
            BaseResult fail = BaseResult.getFail();
            fail.setMsg(err.toString());
            return fail;
        }

        return BaseResult.getSuccess();
    }

    @GetMapping("setMail")
    public BaseResult setMail(@AnnoLogin LoginUser loginUser, @RequestParam("mail") String mail, @RequestParam("pwd")String pwd){
        ReportUser user = new ReportUser();
        user.setId(loginUser.getCurrentUser().getId());
        user.setMailName(mail);
        user.setMailPwd(pwd);
        reportUserMapper.updateByPrimaryKeySelective(user);
        return  BaseResult.getSuccess();
    }

    @GetMapping("test")
    public Object test(){

        Page page = new Page(1,20);

        IPage<BugDto> bugDtoIPage = bugDao.selectPage(page, null);

        return bugDtoIPage;
    }


}
